<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------

namespace plugins\code_generate\validate;

use think\Validate;

class FormValidate extends Validate
{
    protected $rule = [
        'name' => 'require',
        'controllers'=> 'require',
        'model' => 'require|isModel',

    ];

    protected $message = [
        'name.require' => '表单名称不能为空',
        'controllers.require' => '控制器名称不能为空',
        'model.require'   => '模型名称不能为空',

    ];

    protected $scene = [

    ];

    /**
     * @param $value 字段值
     * @param $rules
     * @param $data
     * @param $field
     * @return mixed 字段名
     * @throws \think\db\exception\BindParamException
     * @throws \think\exception\PDOException
     */
    protected function isModel($value , $rules , $data , $field)
    {
        //验证表是否存在
        //这是要查询的表的原始名称【没有加配置前缀】，$data['table_name']。
        $tableName=config('database.prefix').$value;
        $isTable=db()->query('SHOW TABLES LIKE '."'".$tableName."'");
        if($isTable == false)
        {
            return $value.'表不在';
        }
        return true;
    }
}
